package com.ckv.qcm.model.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.ckv.qcm.model.Result;

public class ResultDAO extends DAO {

	
	public ArrayList<Result> find(long idQuestion,long idSession, long idStagiaire) throws SQLException
	{
		ArrayList<Result> ret =new ArrayList<Result>();
		
		try {
			ResultSet res=this.connection.createStatement().executeQuery(
					"SELECT idResultat,idQuestion,idStagiaire,idSession,Bonne FROM RESULTATS"
					+ " WHERE idQuestion=" + idQuestion + " AND idStagiaire=" + idStagiaire + 
					" AND idSession=" + idSession);
			
			while (res.next())
			{
				Result newResult=this.find(res.getLong("idQuestion"),res.getLong("idStagiaire"),res.getLong("idSession"),res.getLong("idReponse"));
//				newResult.setIdQuestion(res.getLong("idQuestion"));
//				newResult.setIdTrainee(res.getLong("idStagiaire"));
//				newResult.setIdAnswer(res.getLong("idReponse"));
//				newResult.setIdSession(res.getLong("idSession"));
//				newResult.setIdResult(res.getLong("idResultat"));
//				newResult.setGood(res.getBoolean("Bonne"));
				
				ret.add(newResult);
				
			}
			
			
			
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}
		
		return ret;
	}
		
	public Result find(long idQuestion,long idTrainee,long idSession,long idReponse) throws SQLException {
		Result ret = new Result();
		try {
			ResultSet res = this.connection.createStatement().executeQuery(
					"SELECT idQuestion,idStagiaire,idSession,idReponse,Bonne FROM RESULTATS"
							+ " WHERE idQuestion=" + idQuestion + " AND idStagiaire="+ idTrainee + " AND idSession=" +idSession + " AND idReponse="+ idReponse);

			if (res.next()) {
				ret = new Result(
						res.getLong("idQuestion"),res.getLong("idReponse"), res.getLong("idStagiaire"),
						res.getLong("idSession"), res.getBoolean("Bonne"));
						
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;

		}

		return ret;
	}

	public Result saveResult(Result result) {
		PreparedStatement preparedStatement;
		ResultSet generatedKeys;

		try {
			preparedStatement = this.connection
					.prepareStatement(
							"INSERT INTO RESULTATS (idQuestion,idStagiaire,idSession,idReponse,Bonne) values"
									+ " (?,?,?,?,?)");

			preparedStatement.setLong(1, result.getIdQuestion());
			preparedStatement.setLong(2, result.getIdTrainee());
			preparedStatement.setLong(3, result.getIdSession());
			preparedStatement.setLong(4, result.getIdAnswer());
			preparedStatement.setBoolean(5, result.isGood());

			int affectedRows = preparedStatement.executeUpdate();
			if (affectedRows == 0) {
				throw new SQLException(
						"Creating result failed, no rows affected.");
			}
		

	 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;

	}

}
